package cn.broadmax.boot.modules.sys.controller;


import cn.broadmax.boot.modules.sys.entity.SysRole;
import cn.broadmax.boot.base.SuperController;
import cn.broadmax.boot.base.message.SResult;
import cn.broadmax.boot.log.SysLog;
import cn.broadmax.boot.modules.sys.bean.SysRoleSearch;
import cn.broadmax.boot.modules.sys.bean.role.RoleCreateRequest;
import cn.broadmax.boot.modules.sys.bean.role.RoleUpdateRequest;
import cn.broadmax.boot.modules.sys.entity.SysUser;
import cn.broadmax.boot.modules.sys.mapper.SysUserMapper;
import cn.broadmax.boot.modules.sys.service.ISysRoleService;
import cn.broadmax.boot.modules.sys.service.ISysUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author jexx
 * @since 2020-07-07
 */
@Api(tags = "角色")
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys/role")
public class SysRoleController extends SuperController {

    private final ISysRoleService sysRoleService;
    private final ISysUserService sysUserService;

    @Autowired
    private final SysUserMapper sysUserMapper;

    @ApiOperation("新增角色")
    @SysLog(value = "新增角色")
    @PutMapping(value = "/insert")
    public SResult<SysRole> addSysRole(@RequestBody RoleCreateRequest request){
        SysRole sysRole = sysRoleService.addRole(request);
        return SResult.ok("", sysRole);
    }

    @ApiOperation("根据主键删除角色")
    @SysLog(value = "删除角色")
    @DeleteMapping(value = "/delete/{roleId}")
    public SResult<Boolean> deleteRoleById(@PathVariable String roleId){
        boolean success = sysRoleService.removeById(roleId);
        return SResult.ok("", success);
    }

    @ApiOperation("根据主键更新角色, 角色code不能更新")
    @SysLog(value = "更新角色")
    @PutMapping(value = "/update/{roleId}")
    public SResult<SysRole> updateRoleById(@PathVariable String roleId, @RequestBody RoleUpdateRequest request){
        sysRoleService.updateRoleById(roleId, request);
        SysRole sysRole = sysRoleService.getById(roleId);
        return SResult.ok("", sysRole);
    }

    @ApiOperation("分页查询角色")
    @GetMapping(value = "")
    public SResult<IPage<SysRole>> pageRoles(SysRoleSearch search){
        LambdaQueryWrapper<SysRole> wrapper = SysRoleSearch.createWrap(search);
        IPage<SysRole> page = sysRoleService.page(getPage(), wrapper);
        return SResult.ok("", page);
    }

    @ApiOperation("获取角色列表")
    @GetMapping(value = "/list")
    public SResult<List<SysRole>> listRoles(SysRoleSearch search){
        LambdaQueryWrapper<SysRole> wrapper = SysRoleSearch.createWrap(search);
        List<SysRole> roles = sysRoleService.list(wrapper);
        return SResult.ok("", roles);
    }

    @ApiOperation("获取用户列表")
    @GetMapping(value = "/getUserName")
    public SResult<List<SysUser>> listUsers( ){
        ArrayList<SysUser> nameList = new ArrayList<>();
        List<SysUser> sysUsers = sysUserMapper.selectList(null);
        for (SysUser sysUser : sysUsers) {
            nameList.add(sysUser);
        }
        return SResult.ok("", nameList);
    }


}
